{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d5e9007b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "812eed15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dotenv:  E:\\work\\code\\ai\\.env\n"
     ]
    }
   ],
   "source": [
    "def get_openai_key():\n",
    "    print(\"dotenv: \",find_dotenv())\n",
    "    _ = load_dotenv(find_dotenv())\n",
    "    return OpenAI()\n",
    "\n",
    "client = get_openai_key()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8c3ef3c3-2d0e-4c5d-942b-bde3a5b2a81a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChatCompletionMessage(content='提供清晰、具体的指示可以引导模型朝向所需的输出，避免无关或不正确的响应。', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None)\n"
     ]
    }
   ],
   "source": [
    "text=f\"\"\"\n",
    "您应该提供尽可能清晰、具体的指示，以表达您希望模型执行的任务。\\\n",
    "这将引导模型朝向所需的输出，并降低收到无关或不正确响应的可能性。\\\n",
    "不要将写清晰的提示词与写简短的提示词混淆。\\\n",
    "在许多情况下，更长的提示词可以为模型提供更多的清晰度和上下文信息，从而导致更详细和相关的输出。\n",
    "\"\"\"\n",
    "# 需要总结的内容\n",
    "prompt =f\"\"\"\n",
    "把用三个反引号括起来的文本总结成一句话。\n",
    "```{text}```\n",
    "\"\"\"\n",
    "def get_completion(prompt, model=\"gpt-3.5-turbo\",temperature = 0):\n",
    "    response = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages= [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": prompt,\n",
    "            }\n",
    "    ],\n",
    "        temperature=temperature\n",
    "    )\n",
    "    return response.choices[0].message\n",
    "# 指令内容，使用 ``` 来分隔指令和待总结的内容\n",
    "response = get_completion(prompt,model=\"gpt-3.5-turbo\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "799a7830-67aa-4f7d-8718-eecb0d335a8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChatCompletionMessage(content='[\\n    {\\n        \"book_id\": 1,\\n        \"title\": \"幻境之门\",\\n        \"author\": \"夜色风华\",\\n        \"genre\": \"奇幻\"\\n    },\\n    {\\n        \"book_id\": 2,\\n        \"title\": \"星际迷航\",\\n        \"author\": \"银河探索者\",\\n        \"genre\": \"科幻\"\\n    },\\n    {\\n        \"book_id\": 3,\\n        \"title\": \"魔法学院\",\\n        \"author\": \"星辰大师\",\\n        \"genre\": \"魔幻\"\\n    }\\n]', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None)\n"
     ]
    }
   ],
   "source": [
    "prompt =f\"\"\"\n",
    "请生成包括书名、z作者和类别的三本虚构的、非真实的中文书籍，\\\n",
    "并以JSON格式提供，包含以下键： book_id、title、 author、 genre.\n",
    "\n",
    "\"\"\"\n",
    "def get_completion(prompt, model=\"gpt-3.5-turbo\",temperature = 0):\n",
    "    response = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages= [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": prompt,\n",
    "            }\n",
    "    ],\n",
    "        temperature=temperature\n",
    "    )\n",
    "    return response.choices[0].message\n",
    "# 指令内容，使用 ``` 来分隔指令和待总结的内容\n",
    "response = get_completion(prompt,model=\"gpt-3.5-turbo\")\n",
    "pprint(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2c7b3089-39c0-4136-b1f1-5aebe91e05db",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_completion(prompt, model=\"gpt-3.5-turbo\",temperature = 0):\n",
    "    response = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages= [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": prompt,\n",
    "            }\n",
    "    ],\n",
    "        temperature=temperature\n",
    "    )\n",
    "    return response.choices[0].message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "099face5-a6e5-48de-93b3-2c4579bae9b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text 1 的总结\n",
      "第一步 - 把水烧开。\n",
      "第二步 - 拿一个杯子并把茶包放进去。\n",
      "第三步 - 把烧开的水倒在茶包上。\n",
      "第四步 - 等待一会儿，让茶叶浸泡。\n",
      "第五步 - 取出茶包。\n",
      "第六步 - 如果愿意，可以加一些糖或牛奶调味。\n",
      "未提供步骤\n"
     ]
    }
   ],
   "source": [
    "text_1 = f\"\"\"\n",
    "泡一杯茶很容易。首先，需要把水烧开。\\\n",
    "在等待期间，拿一个杯子并把茶包放进去。\\\n",
    "一旦水足够热，就把它倒在茶包上。\\\n",
    "等待一会儿，让茶叶浸泡。几分钟后，取出茶包。\\\n",
    "如果您愿意，可以加一些糖或牛奶调味。\\\n",
    "就这样，您可以享受一杯美味的茶了。\n",
    "\"\"\"\n",
    "prompt= f\"\"\"\n",
    "您将获得由三个引号括起来的文本。\\\n",
    "如果它包含一系列的指令，则需要按照以下格式重新编写这些指令：\n",
    "\n",
    "第一步 - ...\n",
    "第二步 - ...\n",
    "第三步 - ...\n",
    "...\n",
    "第n步 - ...\n",
    "\n",
    "如何文本中不包含一些列指令， 则直接写\"未提供步骤\"。\"\n",
    "```{text_1}```\n",
    "\"\"\"\n",
    "\n",
    "response = get_completion(prompt)\n",
    "print(\"text 1 的总结\")\n",
    "print(response.content)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4f2c0ebb-6e12-4460-b584-45fb6fd6b793",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<祖父>：韧性就像是一根弹簧，能够承受压力和挑战，但不会轻易折断；就像是一棵参天大树，经历风雨却依然挺立不倒；就像是一块坚硬的石头，经受岁月洗礼却依然坚不可摧。\n"
     ]
    }
   ],
   "source": [
    "prompt =f\"\"\"\n",
    "您的任务是一一致的风格回答问题\n",
    "<孩子>：请教我何为耐心。\n",
    "<祖父>： 挖出最深峡谷的河流源于一处不起眼的泉眼；最宏伟的交响乐从单一的音符开始；最复杂的挂毯以一根孤独的线开始编织。\n",
    "<孩子>： 请教我何为韧性。\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "40399e9b-0e83-474f-9a18-ff8540a1acad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "prompt 1: \n",
      "1. 兄妹在山顶井里打水时发生意外，但他们的冒险精神依然不减。\n",
      "2. In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. As they climbed while singing joyfully, Jack tripped on a stone and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with joy.\n",
      "3. Jack, Jill\n",
      "4. {\n",
      "    \"english_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. As they climbed while singing joyfully, Jack tripped on a stone and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with joy.\",\n",
      "    \"num_names\": 2\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "text = f\"\"\"\n",
    "在一个迷人的村庄里，兄妹杰克和吉尔出发去一个山顶井里打水。\\\n",
    "他们一边唱着欢乐的歌，一边往上爬，\\\n",
    "然而不幸降临——杰克绊了一块石头，从山上滚了下来，吉尔紧随其后。\\\n",
    "虽然略有些摔伤，但他们还是回到了温馨的家中。\\\n",
    "尽管出了这样的意外，他们的冒险精神依然没有减弱，继续充满愉悦地探索。\n",
    "\"\"\"\n",
    "prompt=f\"\"\"\n",
    "请执行以下操作：\n",
    "1. - 用一句话概括下面三个反引号括起来的文字\n",
    "2. - 将摘要翻译成英语\n",
    "3. - 在英语摘要中列出每个人名\n",
    "4. - 输出一个JSON对象，其中包含以下键：english_summary, num_names.\n",
    "\n",
    "请用换行符分隔您的答案。\n",
    "```{text}```\n",
    "\"\"\"\n",
    "reponse = get_completion(prompt)\n",
    "print(\"prompt 1: \")\n",
    "print(reponse.content)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "77c10deb-334a-40d8-ab2d-e6d19e79f8ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际解决方案和步骤：\n",
      "总费用 = 土地费用 + 电池板费用 + 维护费用\n",
      "总费用 = 100x + 250x + 100,000 + 10x\n",
      "总费用 = 360x + 100,000\n",
      "\n",
      "学生计算的总费用：450x + 100,000美元\n",
      "实际计算的总费用：360x + 100,000美元\n",
      "学生计算的费用和实际计算的费用是否相同：否\n",
      "学生的解决方案和实际解决方案是否相同：否\n",
      "学生的成绩：不正确\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "判断学生的解决方案是否正确。\n",
    "使用以下格式：\n",
    "\n",
    "    问题：问题文本\n",
    "    学生的解决方案：学生的解决方案文本\n",
    "    实际解决方案和步骤：实际解决方案和步骤文本\n",
    "    学生计算的总费用：学生计算得出的总费用\n",
    "    实际计算的总费用：实际计算得出的总费用\n",
    "    学生计算的费用和实际计算的费用是否相同：是或否\n",
    "    学生的解决方案和实际解决方案是否相同：是或否\n",
    "    学生的成绩：正确或不正确\n",
    "    \n",
    "问题:\n",
    "我正在建造一个太阳能发电站，需要帮助计算财务。\n",
    "\n",
    "    土地费用为 100美元/平方英尺\n",
    "    我可以以 250美元/平方英尺的价格购买太阳能电池板\n",
    "    我已经谈判好了维护合同，每年需要支付固定的10万美元，并额外支付每平方英尺10美元\n",
    "    作为平方英尺数的函数，首年运营的总费用是多少。\n",
    "\n",
    "学生的解决方案：\n",
    "设x为发电站的大小，单位为平方英尺。\n",
    "费用：\n",
    "\n",
    "    土地费用：100x\n",
    "    太阳能电池板费用：25\n",
    "    维护费用：100,000美元+100x\n",
    "    总费用：100x+250x+100,000美元+100x=450x+100,000美元\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response.content)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7e3c052a-7587-4692-96bd-1a22b33645d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小米生产的GTs Watch 运动手表是一款智能运动手表，具有多项功能。它配备了心率监测、睡眠监测、运动跟踪、GPS定位等功能，可以帮助用户全面了解自己的健康状况和运动数据。此外，GTs Watch 运动手表还支持智能提醒、消息推送、音乐控制等功能，让用户在运动中也能保持联系和享受音乐。它还具有防水功能，可以在游泳等水上运动中使用。总体来说，小米GTs Watch 运动手表是一款功能丰富、性价比高的智能运动手表。\n"
     ]
    }
   ],
   "source": [
    "prompt= f\"\"\"\n",
    "请告诉我小米生产的GTs watch 运动手表相关信息\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c319134c-36fd-491f-9aa7-e610f3317d5b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
